package com.dy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.entity.Order;
import com.dy.entity.OrderVo;
import com.dy.entity.Sale;
import com.dy.service.IFeignOrderService;
import com.dy.service.ISaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dy
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/sell")
@Api(tags = "销售系统操作接口")
public class SaleController {
    @Autowired
    private ISaleService saleService;
    @Qualifier("com.dy.service.IFeignOrderService")
    @Autowired
    private IFeignOrderService feignOrderService;

    @GetMapping("/selectAllSale/{pageNum}/{pageSize}")
    @ApiOperation("查询单页销售电影票")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页面展示条数")
    })
    public Page<Sale> selectAllSale(@PathVariable("pageNum")Integer pageNum,
                                    @PathVariable("pageSize")Integer pageSize){
        Page<Sale> page = new Page<>(pageNum-1,pageSize);
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();
        wrapper.eq("sale_state",0l);
        return saleService.page(page,wrapper);
    }

    @PostMapping("/buy")
    @ApiOperation("购票")
    public String buy(OrderVo orderVo){
        Sale sale = new Sale();
        if (orderVo.getSaleNumber() < orderVo.getOrderNum()){
            return null;
        }
        sale.setCinameNum(orderVo.getSaleNumber()- orderVo.getOrderNum());
        sale.setSaleNum(orderVo.getOrderNum());
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();
        wrapper.eq("ciname_name",orderVo.getCinemaName());
        boolean update = saleService.update(sale,wrapper);
        if (!update){
            return null;
        }
        String orderSerial = feignOrderService.insertOrder(orderVo);
        if (orderSerial != null){
            return orderSerial;
        }
        return null;
    }

    @GetMapping("/tickets/{cinemaName}")
    public boolean tickets(@PathVariable("cinemaName")String cinemaName){
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();
        wrapper.eq("ciname_name",cinemaName);
        Sale one = saleService.getOne(wrapper);
        if (one.getCinameNum() > 0){
            one.setCinameNum(one.getCinameNum() - 1);
            one.setSaleNum(one.getSaleNum() + 1);
            return saleService.update(one, wrapper);
        }
        return false;
    }

    @GetMapping("/redisCinema")
    public List<Sale> redisCinema(){
        List<Sale> list = saleService.list();
        return list;
    }

    @GetMapping("/selectSaleBySerial/{serial}")
    public Sale selectSaleBySerial(@PathVariable("serial")String serial){
        QueryWrapper<Sale> wrapper = new QueryWrapper<>();
        wrapper.eq("ciname_serial",serial);
        return saleService.getOne(wrapper);
    }

    @PostMapping("/shakedown")
    @ApiOperation("抢票")
    public String shakedown(OrderVo orderVo){
        System.out.println(orderVo);
        return feignOrderService.shakedown(orderVo);
    }
}
